﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Security.Principal;
using Jink.Protocol.Core;
using Jink.Protocol.Core.Verbs;
using XenoGears.Collections;
using XenoGears.Collections.Dictionaries;
using XenoGears.Functional;
using XenoGears.Strings;

namespace Jink.Core
{
    public class CoreRequest : IRequest
    {
        private readonly IRequestMedia _media;
        public IRequestMedia Media { get { return _media; } }

        public IPrincipal User { get; private set; }
        public String Resource { get; private set; }
        public Verb Verb { get; private set; }
        public ReadOnlyDictionary<String, String> Arguments { get; private set; }

        public CoreRequest(IRequestMedia media)
        {
            _media = media;

            User = media.User;
            Resource = media.RelativeUrl;
            Verb = media.Verb;

            var args = new Dictionary<String, String>();
            args.AddElements(media.Form);
            args.AddElements(media.Cookies);
            args.AddElements(media.Headers == null ? null : 
                media.Headers.ToDictionary(kvp => kvp.Key.ToString().Uncapitalize(), kvp => kvp.Value));
            Arguments = args.ToReadOnly();
        }

        public IResponse Respond()
        {
            return new CoreResponse(this);
        }
    }
}
